package com.yjf.ljr.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.adapter.BusinessManageAdapter;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.BusinessManageDateClass;
import com.yjf.ljr.dataclass.BusinessManageDateClass.BusinessManageInfo;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.view.XListView;
import com.yjf.ljr.view.XListView.IXListViewListener;

@SuppressLint("SimpleDateFormat")
public class BusinessManagementActivity extends IjActivity {
	private BusinessManageAdapter mBusinessManageAdapter;
	private XListView mxListViewBusinessManage;
	private List<BusinessManageInfo> mData = new ArrayList<BusinessManageInfo>();
	private final int mPageSize = 10;
	private int mCurPage = 1;
	private boolean mIsGettingData = false;
	private TextView mTvAllAmount;// 个人收益合计
	@ID("fail_business_management")
	private LinearLayout mLlfail_business_management;
	@ID("llbusiness_management")
	private LinearLayout mLlbusiness_management;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_business_management);
		initControl();
		showProgressDialog();
		new BusinessManageTask(mPageSize, mCurPage, "", "", false).execute();
	}

	private void initControl() {
		setLeftBtnClick();
		setTitleStr("业务管理");
		mTvAllAmount = (TextView) findViewById(R.id.tvallamount);
		mxListViewBusinessManage = (XListView) findViewById(R.id.xlvBusinessMange);
		mBusinessManageAdapter = new BusinessManageAdapter(this, mData);
		mxListViewBusinessManage.setAdapter(mBusinessManageAdapter);
		mxListViewBusinessManage.setPullLoadEnable(true);
		mxListViewBusinessManage.setPullRefreshEnable(true);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mxListViewBusinessManage.setRefreshTime(simpleDateFormat.format(new Date()));
		mxListViewBusinessManage.mFooterView.hide();
		mxListViewBusinessManage.setXListViewListener(new MyXListViewListener());
		mxListViewBusinessManage.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub

			}
		});
	}

	private class MyXListViewListener implements IXListViewListener {

		@Override
		public void onRefresh() {
			// TODO Auto-generated method stub
			mCurPage = 1;
			loadingData(mPageSize, mCurPage, false, false);
		}

		@Override
		public void onLoadMore() {
			// TODO Auto-generated method stub
			loadingData(mPageSize, ++mCurPage, true, false);
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// TODO Auto-generated method stub
		if (resultCode == RESULT_OK) {

			switch (requestCode) {
			case CommonData.REQUEST_CODE_LOGIN:
				mCurPage = 1;
				loadingData(mPageSize, mCurPage, false, true);
				break;

			default:
				break;
			}

		}
		super.onActivityResult(requestCode, resultCode, intent);
	}

	class BusinessManageTask extends AsyncTask<Void, Void, Void> implements OnClickListener {

		private String mFailStr;
		private int curPage;
		private int pageSize;
		private boolean isadd = false;
		private BusinessManageDateClass dc = new BusinessManageDateClass();

		public BusinessManageTask(int pageSize, int curPage, String dateprv, String datenext, boolean isadd) {
			super();
			this.curPage = curPage;
			this.pageSize = pageSize;
			this.isadd = isadd;
		}

		@Override
		protected void onPreExecute() {
			mIsGettingData = true;
		}

		@Override
		protected Void doInBackground(Void... param) {
			// TODO Auto-generated method stub
			RequestObject requestObject = new RequestObject();
			requestObject.method = "appBusinessManager";
			// requestObject.map.put("userId", "2371");
			requestObject.map.put("pageNumber", curPage);
			requestObject.map.put("pageSize", pageSize);
			String requrestUrl = RequestBuilder.build(requestObject);
			try {
				IjConnect.doRequest(requrestUrl, dc);
				if (dc.code == null) {
					mFailStr = getResources().getString(R.string.loaddata_exception);
				} else if (!dc.code.equals(CommonData.RESULT_SUCCESS)) {
					mFailStr = dc.msg;
				}
			} catch (Exception e) {
				// TODO: handle exception
				mFailStr = getResources().getString(R.string.loaddata_exception);
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			if ("-1".equals(dc.code)) {
				loginWebview();
				return;
			}
			if (!TextUtils.isEmpty(mFailStr)) {
				mLlfail_business_management.setVisibility(View.GONE/* VISIBLE */);
				mLlfail_business_management.setOnClickListener(this);
				mLlbusiness_management.setVisibility(View.GONE);
				showToast(mFailStr);
			} else {
				mLlfail_business_management.setVisibility(View.GONE);
				mLlbusiness_management.setVisibility(View.VISIBLE);
				mTvAllAmount.setText("佣金总额：" + dc.totalMoney + "元");
				if (!isadd) {
					mData.clear();
				}
				if (dc.salesList != null && dc.salesList.size() > 0) {

					mData.addAll(dc.salesList);
					mBusinessManageAdapter.notifyDataSetChanged();

					if (mData.size() <= (dc.totalPage - 1) * pageSize) {
						mxListViewBusinessManage.mFooterView.show();
					} else {
						mxListViewBusinessManage.mFooterView.hide();
					}
				}

			}
			mxListViewBusinessManage.stopLoadMore();
			mxListViewBusinessManage.stopRefresh();
			mIsGettingData = false;
			dismissProgressDialog();
		}

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.fail_business_management:
				showProgressDialog();
				new BusinessManageTask(pageSize, curPage, "", "", false).execute();
				break;
			default:
				break;
			}
		}
	}

	private void loadingData(int pageSize, int curPage, boolean isadd, boolean showProgress) {
		if (mIsGettingData) {
			return;
		}

		if (showProgress) {
			showProgressDialog();
		}
		new BusinessManageTask(pageSize, curPage, "", "", isadd).execute();
	}

}
